#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
from itertools import islice

def format_num(n):
    if n<10:
        return "0"+str(n)
    return str(n)

#1906070829
def format_time(s):
    return "20"+s[0]+s[1]+"-"+s[2]+s[3]+"-"+s[4]+s[5]+" "+s[6]+s[7]+":"+s[8]+s[9]+":00"


def gen_sql(fn,gwNum):

    with open(fn, "r", encoding='utf-8') as f:
        count=0
        fileIndex = 1
        wf=open(fn+"_"+format_num(fileIndex)+".sql", "w", encoding='utf-8')
        while 1:
            size=1024*1024#一次读取1M
            lines=f.readlines(size)
            if not lines:
                break
            # print(len(lines))

            for line in lines:
                line=line.strip()
                # line=line.replace(rp,"exec")
                # print(line)
                searchObj = re.search(rp, line, re.M | re.I)

                if searchObj:
                    msgId=searchObj.group(1)
                    stat=searchObj.group(2)
                    submitTime=format_time(searchObj.group(3))
                    doneTime=format_time(searchObj.group(4))
                    mobile=searchObj.group(6)
                    seqId=searchObj.group(7)
                    # print(format_time(submitTime))

                    sql="insert into sms_status(msgId, stat, submitTime, doneTime, mobile, seqId, gwNum) VALUES ('"+msgId+"', '"+stat+"', '"+submitTime+"', '"+doneTime+"', '"+mobile+"', '"+seqId+"',"+str(gwNum)+")"
                    # print(sql)
                    wf.write(sql)
                    wf.write(";\n")
                    count = count + 1
                else:
                    print("Nothing found!!")
                    print(line)



                if(count>=100000):
                    count=0
                    fileIndex=fileIndex+1
                    wf.close()
                    wf = open(fn+"_" + format_num(fileIndex)+".sql", "w", encoding='utf-8')

               # print(line.strip())

        if not wf.closed:
            wf.close()

fn=r'C:\Users\andnn\Desktop\status\status_10468_20190607.log'
gen_sql(fn,10468)

fn=r'C:\Users\andnn\Desktop\status\status_10469_20190607.log'
gen_sql(fn,10469)